
## ----loadlibs, echo = FALSE, message = FALSE-----------------------------
library(nnet)
library(stargazer)
library(ascii)
library(pscl)


## ----loaddata, echo = FALSE, message = FALSE-----------------------------
dat <- read.csv("lab_leadership_data_tidied.csv")
candidates <- c("ABBOTT..Diane",
	"BALLS..Ed",
	"BURNHAM..Andy",	
	"MILIBAND..David",
	"MILIBAND..Ed")

candidates.pretty <- c("Abbott","Balls","Burnham",
	"Miliband, D","Miliband, E")



## ----crosstab, echo = FALSE, results = "hide"----------------------------
table(CLP = dat$FirstPref.clp[which(dat$FirstPref!="None")],
	MP = dat$FirstPref[which(dat$FirstPref!="None")])


## ----plot1, fig = TRUE, dpi = 300, echo = FALSE, width = 7, height = 4, fig.cap = "Impact of endorsement by sitting MP on candidate vote share."----
shares <- numeric(0)
sds <- numeric(0)
for (i in candidates) {
	tmp <- c(mean(dat[which(dat$LabourListEndorsement != i),paste0(i,".clp.pct")],na.rm = TRUE),
		mean(dat[which(dat$LabourListEndorsement == i),paste0(i,".clp.pct")],na.rm = TRUE))
	shares <- c(shares, tmp)
	tmp <- c(sd(dat[which(dat$LabourListEndorsement != i),paste0(i,".clp.pct")],na.rm = TRUE),
		sd(dat[which(dat$LabourListEndorsement == i),paste0(i,".clp.pct")],na.rm = TRUE))
	sds <- c(sds, tmp)
}
plotMat <- 100 * matrix(shares, ncol = 5, byrow = FALSE)
### plot this
myBarplot <- barplot(plotMat,
	beside = TRUE,
	names.arg = candidates.pretty,
	col = c("gray","black"),
	border = NA,
	xlab = "",
	ylim = c(0, 70),
	ylab = "Avg. constituency vote share (%)")

### Add the error-bars
#segments(myBarplot, plotMat - (sds*100), myBarplot, plotMat + (sds*100), lwd=2, col = "red")
#segments(myBarplot - 0.25, plotMat - (sds*100), myBarplot + 0.25, plotMat - (sds*100), lwd=2, col = "red")
#segments(myBarplot - 0.25, plotMat + (sds*100), myBarplot + 0.25, plotMat + (sds*100), lwd=2, col = "red")

### Add the text

myText <- round(apply(plotMat, 2, diff),1)
myText <- paste0("+",myText)
text(colMeans(myBarplot), apply(plotMat,2,max) + (apply(matrix(sds,ncol = 2,byrow = T),1,max)*100),
	myText,
	pos = 3, cex = 1) 


## ----model, echo = FALSE, message = FALSE, warning = FALSE, results = "hide"----
dat$LabourListEndorsement <- factor(dat$LabourListEndorsement)
dat$LabourListEndorsement <- relevel(dat$LabourListEndorsement, "None")

dat$FirstPref <- factor(dat$FirstPref)
dat$FirstPref <- relevel(dat$FirstPref, "None")
dat$SecondPref <- factor(dat$SecondPref)
dat$SecondPref <- relevel(dat$SecondPref, "None")
dat$ThirdPref <- factor(dat$ThirdPref)
dat$ThirdPref <- relevel(dat$ThirdPref, "None")
dat$FourthPref <- factor(dat$FourthPref)
dat$FourthPref <- relevel(dat$FourthPref, "None")

dat$scotland <- as.numeric(dat$region=="Scotland")
dat$wales <- as.numeric(dat$region=="Wales")

y <- as.matrix(dat[,paste0(candidates,".clp")])
### Best re-arrange these so Miliband D is first
y <- y[,c(4,1:3,5)]

nullmod <- multinom(y ~ 1, data = dat)

mmod4 <- multinom(y ~ con10 + lab10 +ld10 + lab.maj + scotland + wales + taxspend.hat + 
	eudisapp.hat + LabourListEndorsement + SecondPref + ThirdPref + FourthPref, data = dat)

mmod.full <- update(mmod4, . ~ . + VOTES.CAST.clp + TURNOUT.clp + log.earn + nonwhite + relig.christian + relig.refused + relig.other + age + private + owns + female + married + education + socgrd + log.density + region, maxit = 1000)

mcfadden <- pR2(mmod4)

AIC(mmod4)
AIC(mmod.full)


## ----modout, echo = FALSE, results = "asis"------------------------------
stargazer(mmod4, 
	dep.var.labels = c("Abbott / \nMiliband D","Balls / \nMiliband D","Burnham / \nMiliband D","Miliband E / \nMiliband D"),
	covariate.labels = c("Conservative vote share","Labour vote share","Lib Dem vote share",
		"Lab. majority/shortfall",
		"Scottish constituency", "Welsh constituency",
		"Support for taxation and spending",
		"Euroskepticism",
		"Endorsed Abbott","Endorsed Balls","Endorsed Burnham","Endorsed David Miliband","Endorsed Ed Miliband",
		"2nd pref Abbott","2nd pref Balls","2nd pref Burnham","2nd pref David Miliband","2nd pref Ed Miliband",
		"3rd pref Abbott","3rd pref Balls","3rd pref Burnham","3rd pref David Miliband","3rd pref Ed Miliband",
		"4th pref Abbott","4th pref Balls","4th pref Burnham","4th pref David Miliband","4th pref Ed Miliband"),
	header = FALSE,font.size = "tiny", single.row = TRUE,
	star.cutoffs = c(0.05, 0.01, 0.001))


## ----fds, echo = FALSE, results = "asis"---------------------------------
### (1) Set up the first scenario, with everything at its mean
indf <- colMeans(dat[,c("con10","lab10","ld10","lab.maj","scotland","wales","taxspend.hat","eudisapp.hat",
	"VOTES.CAST.clp","TURNOUT.clp","log.earn","nonwhite","relig.christian","relig.refused","relig.other",
	"age","private","owns","female","married","education","socgrd","log.density")],na.rm = T)
indf <- data.frame(t(indf))
indf$scotland <- 0
indf$wales <- 0
indf$LabourListEndorsement <- "None"
### (2) Add on scenarios with different first preferences
indf <- rbind(indf, cbind(LabourListEndorsement = levels(dat$LabourListEndorsement)[-1], indf[,-ncol(indf)]))
### (3) SD change in con10
tmp <- indf[1,]
tmp$con10 <- tmp$con10 + sd(dat$con10,na.rm = TRUE)
indf <-  rbind(indf, tmp)
### (4) SD change in lab10
tmp <- indf[1,]
tmp$lab10 <- tmp$lab10 + sd(dat$lab10,na.rm = TRUE)
indf <-  rbind(indf, tmp)
### (5) SD change in ld10
tmp <- indf[1,]
tmp$ld10 <- tmp$ld10 + sd(dat$ld10,na.rm = TRUE)
indf <-  rbind(indf, tmp)
### (6) unit change in scotland
tmp <- indf[1,]
tmp$scotland <- 1
indf <-  rbind(indf, tmp)
### (7) unit change in wales
tmp <- indf[1,]
tmp$wales <- 1
indf <-  rbind(indf, tmp)
### (8) SD change in taxspend.hat
tmp <- indf[1,]
tmp$taxspend.hat <- tmp$taxspend.hat + sd(dat$taxspend.hat,na.rm = TRUE)
indf <-  rbind(indf, tmp)
### (9) SD change in eudisapp.hat
tmp <- indf[1,]
tmp$eudisapp.hat <- tmp$eudisapp.hat + sd(dat$eudisapp.hat,na.rm = TRUE)
indf <-  rbind(indf, tmp)

### Describe these changes
indf$Change <- c("Baseline",
	"Abbott endorsement",
	"Balls endorsement",
	"Burnham endorsement",
	"David Miliband endorsement",
	"Ed Miliband endorsement",
	"Cons. vote +1 SD",
	"Lab vote +1 SD",
	"LD vote +1 SD",
	"Scottish seat",
	"Welsh seat",
	"+1 SD more right-wing on taxation",
	"+1 SD more Euroskeptic")

### Get out the predictions
indf$SecondPref <- "None"
indf$ThirdPref <- "None"
indf$FourthPref <- "None"

preds4 <- predict(mmod4, newdata = indf, "probs")
### Net off the first line
preds4 <- preds4[-1,] - matrix(preds4[1,], ncol = 5, nrow = nrow(preds4)-1, byrow = TRUE)
### put Miliband in order
preds4 <- preds4[,c(2:4,1,5)]
endorsement.vals <- diag(preds4[1:5,])
predsout4 <- cbind(indf$Change[-1], round(preds4*100,1))


## ----predouts, echo = FALSE, results = "asis", message = FALSE, warning = FALSE----
print(ascii(predsout4, 
	caption = "Changes in vote share",
	header = 1,
	colnames = c("Change","Abbott","Balls","Burnham","Miliband, D","Miliband, E"), 
	include.colnames = TRUE),
	type = "pandoc")
